package log_repo

import (
	"context"
	"vmapp/app0/internal/biz/repo"
	"vmapp/app0/internal/conf"
	"vmapp/app0/internal/data/base"
	"vmapp/app0/internal/dto"
	"vmapp/app0/internal/models"

	"github.com/go-kratos/kratos/v2/log"
)

type eventLogRepo struct {
	*base.Data
	log  *log.Helper
}

func NewEventLogRepo(data *base.Data, component *conf.BootComponent) repo.EventLogRepo {
	return &eventLogRepo{
		Data: data,
		log:  component.Logger,
	}
}

func (t *eventLogRepo) CreateEventLog(ctx context.Context, one *models.EventLog) error {
	err := t.DB(ctx).Create(&one).Error
	return err
}

func (t *eventLogRepo) ListEventLog(ctx context.Context, req *dto.ListEventLogReq) (count int64, all []*models.EventLog, err error) {
	if req.Current == 0 {
		req.Current = 1
	}

	if req.Size == 0 {
		req.Size = 10
	}

	err = t.DB(ctx).Model(new(models.EventLog)).Count(&count).Limit(req.Size).Offset((req.Current - 1) * req.Size).Order("id desc").Find(&all).Error
	return
}

func (t *eventLogRepo) ListSendLog(ctx context.Context, req *dto.ListSendLogReq) (count int64, all []*models.Send, err error) {
	if req.Current == 0 {
		req.Current = 1		
	}

	if req.Size == 0 {
		req.Size = 10
	}

	err = t.DB(ctx).Model(new(models.Send)).Count(&count).Limit(req.Size).Offset((req.Current - 1) * req.Size).Order("id desc").Find(&all).Error
	return
}
